import router from '@/router';
import setting from '@/setting';
import nprogress from 'nprogress';
import "nprogress/nprogress.css";
nprogress.configure({ showSpinner: false });

import useUserStore from '@/store/modules/user';
import pinia from '@/store';
let userStore = useUserStore(pinia);

// 全局前置守卫
router.beforeEach(async (to, _from, next) => {
  document.title = `${setting.title} - ${to.meta.title}`;
  nprogress.start();

  // 获取 token
  let token = userStore.token;

  // 用户登录判断
  if (token) {
    // 有 token，允许访问任何页面
    next(); // 继续路由切换
  } else {
    // 没有 token，重定向到登录页面
    if (to.path !== '/login') {
      next({ path: '/login', query: { redirect: to.path } });
    } else {
      next();
    }
  }
});

// 全局后置守卫
router.afterEach(() => {
  nprogress.done();
});